package com.xiaoxian.basic.cotroller;

import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class BaseController {
    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping
    public AjaxResult upload(@RequestPart MultipartFile file) throws IOException {
        //获取字节数组
        byte[] bytes = file.getBytes();
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        String fileName=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        String upload = FastdfsUtil.upload(bytes, fileName);

        return AjaxResult.my().setPath(upload);
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path){
        //获取组名
        String pathTmp = path.substring(1); // group1/M00/00/09/rBEACmKXF8-AUc6KAANsldwx3H4713.jpg
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //  group1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// M00/00/09/rBEACmKXF8-AUc6KAANsldwx3H4713.jpg
//            System.out.println(groupName);
//            System.out.println(remotePath);
        FastdfsUtil.delete(groupName, remotePath);
        return AjaxResult.my();
    }

}
